package leetcode.editor.offer;

import java.util.Arrays;

// 剑指 Offer 61. 扑克牌中的顺子
// https://leetcode.cn/problems/bu-ke-pai-zhong-de-shun-zi-lcof/
class BuKePaiZhongDeShunZiLcof {
    public static void main(String[] args) {
        Solution solution = new BuKePaiZhongDeShunZiLcof().new Solution();
        solution.isStraight(new int[]{0, 0, 1, 2, 5});
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        // 手写排序
        /*public boolean isStraight(int[] nums) {
            quickSort(nums, 0, nums.length - 1);
            int count = 0;  // 0的个数
            for (int i = 0; i < nums.length; i++) {
                if (nums[i] == 0) {
                    count++;
                    continue;
                }
                if ((nums[i] - nums[i - 1]) > 1 && (nums[i] - nums[i - 1]) > count + 1) {
                    return false;
                } else if ((nums[i] - nums[i - 1]) == 1) {
                    continue;
                } else {
                    count = count - (nums[i] - nums[i - 1]);
                }

                if (nums[i] != 0 && nums[i - 1] != 0 && nums[i] - nums[i - 1] == 0) {
                    return false;
                }
            }
            return true;
        }

        public void quickSort(int[] nums, int l, int r) {
            if (l >= r) {
                return;
            }

            int i = l, j = r;
            while (i < j) {
                while (i < j && nums[l] <= nums[j]) j--;
                while (i < j && nums[l] >= nums[i]) i++;
                swap(nums, i, j);
            }

            swap(nums, l, i);
            quickSort(nums, l, i - 1);
            quickSort(nums, i + 1, r);

        }

        public void swap(int[] nums, int i, int j) {
            // 交换 nums[i] 和 nums[j]
            int tmp = nums[i];
            nums[i] = nums[j];
            nums[j] = tmp;
        }*/

        /**
         * 哈希表
         *
         * @param nums
         * @return
         */
        /*public boolean isStraight(int[] nums) {
            // 0直接跳过，找出最大值和最小值
            int max = 0, min = Integer.MAX_VALUE;
            HashSet<Integer> set = new HashSet<>();
            for (int num : nums) {
                if (num == 0) continue;
                max = Math.max(num, max);
                min = Math.min(num, min);
                if (set.contains(num)) return false;
                set.add(num);
            }
            return max - min < 5 ? true : false;
        }*/

        /**
         * 排序
         *
         * @param nums
         * @return
         */
        public boolean isStraight(int[] nums) {
            Arrays.sort(nums);  // 可以自己写快排
            // 大王在前面
            int count = 0;  // 大王的数量
            for (int i = 0; i < 4; i++) {
                if (nums[i] == 0) {
                    count++;   // 大王的数量加一
                    continue;
                }
                if (nums[i] == nums[i + 1]) return false; // 如果后面有重复
            }
            return nums[nums.length - 1] - nums[count] < 5 ? true : false;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
